/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filecopier;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author TQNINH
 */
public class CopyFile {
    
    public CopyFile() {
        
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("**** Welcome to the File Copier Application ****");
        System.out.print("Enter the path of source file along with the filename: ");
        Scanner input = new Scanner(System.in);
        String path = input.next();
        
        File fileName = new File(path);
        String location = path.substring(0, path.indexOf(fileName.getName()));
        
        try {
            FileInputStream fin = new FileInputStream(path);

            System.out.print("Enter the path where file is to be copied: ");
            String copy = input.next();

            if(copy.equals(location)) {
                System.out.println("The source and the destination path are the same!");
                fin.close();
            } else {
                FileOutputStream fout = new FileOutputStream(copy + fileName.getName());
                int data;
                while((data = fin.read()) != -1) {
                    fout.write(data);
                }
                System.out.println("A copy of the file: " + fileName.getName() +
                        " has been successfully created at the location: " +
                        copy + fileName.getName() + "!");
                fin.close();
                fout.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("The file to be copied could not be located!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("An error occurred while readin from "
                    + "the file!");
        }
    }
}
